package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Connectivity.ConnectionManager;

public class CustomerEntry extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3066865284560195548L;

	/**
	 * Constructor of the object.
	 */
	public CustomerEntry() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//name=BANK+OF+BARODA&branch=21&problem1=1%24DESKTOP&DESKTOP=3%24ACER&LAPTOP=0&PRINTER=0&HWPARTS=0&callfault=dfgdfg&callfault=dfgfg&callnature=1&calllockedby=fdgf&calllockedcontact=dfgdf&oldClass=0&submit=submit&status=BACK&branch=callDegree%28branch%29
		String flag = "";
		 boolean result=true;
		 String cust_id = request.getParameter("branch")!=null?request.getParameter("branch"):"";
		 String name = request.getParameter("name")!=null?request.getParameter("name"):"";
		 String callfault1= request.getParameter("callfault1")!=null?request.getParameter("callfault1"):"";
		 String callfault2 = request.getParameter("callfault2")!=null?request.getParameter("callfault2"):"";
		 String callnature = request.getParameter("callnature")!=null?request.getParameter("callnature"):"";
		 String calllockedby = request.getParameter("calllockedby")!=null?request.getParameter("calllockedby"):"";
		 String calllockedcontact = request.getParameter("calllockedcontact")!=null?request.getParameter("calllockedcontact"):"";
		 String complaintid = request.getParameter("complaintid")!=null?request.getParameter("complaintid"):"";
		 String complaintdate = request.getParameter("complaintdate")!=null?request.getParameter("complaintdate"):"";
		 String complainttime = request.getParameter("complainttime")!=null?request.getParameter("complainttime"):"";
		 String submit = request.getParameter("submit")!=null?request.getParameter("submit"):"";
		 String groupcount = request.getParameter("groupcount")!=null?request.getParameter("groupcount"):"";
		 String minward = request.getParameter("minward")!=null?request.getParameter("minward"):"-";
		 String moutward = request.getParameter("moutward")!=null?request.getParameter("moutward"):"-";
		 String []problemlist=null;
		 int gcount=Integer.parseInt(groupcount);
		 String callfault=callfault1+" <br> "+callfault2;
		 String group_id="";
	 		String problem="";
			String ParameterNames = "";
			ArrayList group=new ArrayList();
			ArrayList item=new ArrayList();
			ArrayList itemid=new ArrayList();
			int tokcnt=0;
			for(Enumeration e = request.getParameterNames(); e.hasMoreElements(); )
			{
				ParameterNames = (String)e.nextElement();
				//out.println(ParameterNames + "=<br/>");
				if(ParameterNames.contains("problem"))
				{
				problem=problem+"$"+request.getParameter(ParameterNames);
//				out.println(problem + "<br/>");
				StringTokenizer st=new StringTokenizer(problem,"$");
				while(st.hasMoreElements())
					{
					String grp=(st.nextToken());
					String grp1=(st.nextToken());
					String itemdetails=request.getParameter(grp1);
					System.out.println("---------------- (itemdetails"+(itemdetails));
					if(itemdetails.equalsIgnoreCase("textfield"))
					{}
					
					else if(itemdetails!=(null) || (itemdetails!=("0") || (itemdetails!="0")  ))
					{group.add(grp);
					StringTokenizer st1=new StringTokenizer(itemdetails,"$");						
					String itemi= st1.nextToken();
					String itemna=st1.nextToken();
					if((Integer.parseInt(itemi))>0 )
					{itemid.add(itemi);}//for itemid exists
					}
					
					}
	
				}
			}

			 HttpSession session = request.getSession(true);
			 String created_by=""+(session.getAttribute("uname"));

	
	try
	{	 //Date date = new Date(0000-00-00);
		Date d1=new Date();
	    String currentdate=((d1.getYear()+1900)+"-"+(d1.getMonth()+1)+"-"+d1.getDate());   /*DateFormat formatter ; 
		 Date complaint_date ; 
		  formatter = new SimpleDateFormat("dd-MMM-yy");
		  complaint_date = (Date)formatter.parse(complaintdate);  
		  */
	    if(itemid.size()>1)
	    {itemid.remove(0);}
	    
	    
		System.out.println(d1+"  currentdate :"+currentdate);
		
		
	    Connection con=null;
	    flag=request.getParameter("flag");
	    System.out.println("flag:"+flag);
		
		con=ConnectionManager.getConnection();
		if(  request.getParameter("submit").equalsIgnoreCase("create"))
		{
			
			
	 for (int i=0;i<itemid.size();i++)
	 {
		String query="Insert into callentry(callentry_id,complaint_id,group_id,subgroup_id,item_id,customer_id";
		query=query+",name,contact_no,call_lockdate,call_locktime, call_status,call_details,call_nature_id";
		query=query+",creation_date,created_by,status,minward,moutward";
		query=query+") values(nextval('callentry_seq'),?,?,?,?,?,?,?,?,?,?,?,?,to_date('"+currentdate+"','yyyy-mm-dd'),?,'V',?,?)";
		PreparedStatement prst=con.prepareStatement(query);
	 	prst.setString(1,complaintid);
		prst.setInt(2,Integer.parseInt(""+group.get(i)));
		prst.setInt(3,1);
		prst.setInt(4,Integer.parseInt(""+itemid .get(i)));
		prst.setInt(5,Integer.parseInt(cust_id));
		prst.setString(6,calllockedby);
		prst.setString(7,calllockedcontact);
		prst.setString(8,complaintdate);
		prst.setString(9,complainttime);
		prst.setString(10,"OPEN");
		prst.setString(11,callfault);
		prst.setInt(12,Integer.parseInt(""+(callnature.trim())));
		 prst.setString(13,created_by);
		 prst.setString(14,minward);
		 prst.setString(15,moutward);
		result= prst.execute();
		System.out.println(" Item  :"+itemid .get(i)); 
		System.out.println(" result  :"+result);
		
	 }	System.out.println(" Item Length :"+itemid.size());
	if(result==false)
	{ response.sendRedirect("/Genius/form/general/CallRegisterEntry.jsp?status=I"); //logged-in page}
	}
	
	
		}
		else if(  request.getParameter("submit").equalsIgnoreCase("update"))
			{
			/*String query="update customer_master set customer_name='"+customer_name+"',address1='"+address1+"',phone_no1='"+phone_no1+"'";
			query=query+",phone_no2='"+phone_no2+"',mobile_no='"+mobile_no+"',email_id='"+email_id+"'";
			query=query+",contactperson1='"+contactperson1+"',contactperson2='"+contactperson2+"'";
			query=query+",mobilecontact1='"+mobilecontact1+"',mobilecontact2='"+mobilecontact2+"'";
			query=query+",area='"+area+"' where customer_id="+cust_id+" and status='V'";
			PreparedStatement prst=con.prepareStatement(query);
			prst.executeUpdate();
			*/
			
			
			
			 response.sendRedirect("/Genius/form/customer/CallRegisterEntry.jsp?flag=U&status=update&cust_id="+cust_id);
				}
		else if(  request.getParameter("submit").equalsIgnoreCase("delete") )
				{
		/*	String query="UPDATE customer_master set status='I'";
			query=query+"where customer_id = ";
			PreparedStatement prst=con.prepareStatement(query);
			prst.executeUpdate();
			response.sendRedirect("/Genius/form/general/CallRegisterEntry.jsp?status=D");
			*/	}
		else if(  request.getParameter("submit").equalsIgnoreCase("BACK") )
		{
	 
	response.sendRedirect("/Genius/form/general/CallRegisterEntry.jsp");
		}
	     
	} 
			
			
	catch (Throwable theException) 	    
	{
	     System.out.println(theException); 
	}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String groupcode=null;
		String groupname=null;
	
	try
	{	    

		//groupcode=123&groupname=23423
		groupcode=(request.getParameter("groupcode"));
		groupname=(request.getParameter("groupname"));
	    Connection con=null;
	
		
		con=ConnectionManager.getConnection();
		String query="Insert into item_group_master(group_id,group_code,group_name,status) values(nextval('item_group_master_seq'),?,?,'V')";
		PreparedStatement prst=con.prepareStatement(query);
	 	prst.setString(1,groupcode);
		prst.setString(2,groupname);
	    prst.execute();
	          
	    response.sendRedirect("/Genius/form/general/CallRegisterEntry.jsp"); //logged-in page  
	 
	     
	} 
			
			
	catch (Throwable theException) 	    
	{
	     System.out.println(theException); 
	}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
